package net.esj.basic.core.view.servlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * servlet代理类
 * @author Administrator
 *
 */
public class DelegatingServletProxy extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String targetBean;

	private Servlet proxy;

	public void init() throws ServletException {
		this.targetBean = getInitParameter("targetBean");
		getServletBean();
		proxy.init(getServletConfig());

	}

	public void service(ServletRequest req, ServletResponse res)
			throws ServletException, IOException {
		proxy.service(req, res);
	}

	private void getServletBean() {

		WebApplicationContext wac = WebApplicationContextUtils

		.getRequiredWebApplicationContext(getServletContext());
		
		this.proxy = (Servlet) wac.getBean(targetBean);

	}

}
